package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import bd.Servicios;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class VentanaIngresarTarjeta extends javax.swing.JInternalFrame {
	private JPanel pnPwd;
	private JTextField usuario;
	private JButton btnAceptar;
	private JPasswordField jpw;
	private JButton jButton1;
	private JLabel lbPW;
	private JLabel lbUser;
	private Servicios serv;
	private VentanaPrincipal ventana;

	/**
	 * Auto-generated main method to display this JInternalFrame inside a new
	 * JFrame.
	 */
	public static void main(String[] args) {
		VentanaIngresarTarjeta inst = new VentanaIngresarTarjeta();
		inst.setPreferredSize(new java.awt.Dimension(310, 157));
		inst.setBounds(0, 0, 310, 157);

	}

	public VentanaIngresarTarjeta() {
		super();
		initGUI();
		serv = Servicios.getServicios();
	}

	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(341, 182));
			this.setBounds(0, 0, 341, 182);
			setVisible(true);
			getContentPane().setLayout(null);
			{
				pnPwd = new JPanel();
				AnchorLayout pnPwdLayout = new AnchorLayout();
				pnPwd.setLayout(pnPwdLayout);
				AnchorLayout pnPwdLayout1 = new AnchorLayout();
				getContentPane().add(pnPwd, "North");
				pnPwd.setBounds(0, 0, 339, 157);
				pnPwd.setLayout(pnPwdLayout1);
				{
					jpw = new JPasswordField();
					pnPwd.add(jpw, new AnchorConstraint(372, 789, 519, 361,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL));
					jpw.setPreferredSize(new java.awt.Dimension(145, 23));
					jpw.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							btnAceptarActionPerformed(evt);
						}
					});
					jpw.addKeyListener(new KeyAdapter() {
						public void keyTyped(KeyEvent e) {
							char caracter = e.getKeyChar();

							if (((caracter < '0') || (caracter > '9'))
									&& (caracter != '\b' /*
														 * corresponde a
														 * BACK_SPACE
														 */)) {
								e.consume(); // ignorar el evento de teclado
							}
						}
					});
				}
				{
					usuario = new JTextField();
					pnPwd.add(usuario, new AnchorConstraint(66, 789, 219, 361,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL));
					usuario.setPreferredSize(new java.awt.Dimension(145, 24));
					usuario.addKeyListener(new KeyAdapter() {
						public void keyTyped(KeyEvent e) {
							char caracter = e.getKeyChar();

							if (((caracter < '0') || (caracter > '9'))
									&& (caracter != '\b' /*
														 * corresponde a
														 * BACK_SPACE
														 */)) {
								e.consume(); // ignorar el evento de teclado
							}
						}
					});
				}
				{
					btnAceptar = new JButton();
					pnPwd.add(btnAceptar, new AnchorConstraint(101, 49, 32,
							195, AnchorConstraint.ANCHOR_ABS,
							AnchorConstraint.ANCHOR_ABS,
							AnchorConstraint.ANCHOR_ABS,
							AnchorConstraint.ANCHOR_ABS));
					btnAceptar.setText("Aceptar");
					btnAceptar.setPreferredSize(new java.awt.Dimension(95, 24));
					btnAceptar.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							btnAceptarActionPerformed(evt);
						}
					});
				}
				{
					lbPW = new JLabel();
					pnPwd.add(lbPW, new AnchorConstraint(378, 290, 506, 51,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL));
					lbPW.setText("Pin");
					lbPW.setPreferredSize(new java.awt.Dimension(81, 20));
				}
				{
					lbUser = new JLabel();
					pnPwd.add(lbUser, new AnchorConstraint(60, 290, 213, 51,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL));
					lbUser.setText("Tarjeta");
					lbUser.setPreferredSize(new java.awt.Dimension(81, 24));
				}
				{
					jButton1 = new JButton();
					pnPwd.add(jButton1, new AnchorConstraint(101, 170, 33, 77,
							AnchorConstraint.ANCHOR_ABS,
							AnchorConstraint.ANCHOR_ABS,
							AnchorConstraint.ANCHOR_ABS,
							AnchorConstraint.ANCHOR_ABS));
					jButton1.setText("Cancelar");
					jButton1.setPreferredSize(new java.awt.Dimension(92, 23));
					jButton1.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jButton1ActionPerformed(evt);
						}
					});
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setServ(Servicios s) {
		serv = s;
	}

	private void btnAceptarActionPerformed(ActionEvent evt) {
		if (serv.conectarBD("atm", "atm") == null) {
			if (usuario.getText() == null || usuario.getText().equals("")) {
				JOptionPane.showMessageDialog(this,
						"Falta el numero de la tarjeta.", "Error",
						JOptionPane.ERROR_MESSAGE);

				return;
			}
			if (jpw.getPassword() == null || jpw.getPassword().length == 0) {
				JOptionPane.showMessageDialog(this, "Falta el pin.", "Error",
						JOptionPane.ERROR_MESSAGE);
				return;
			}

			if (!serv.verifTarjeta(Integer.parseInt(this.usuario.getText()),
					Integer.parseInt(new String(this.jpw.getPassword()))))
				JOptionPane.showMessageDialog(this,
						"Error. Tarjeta o pin incorrectos.",
						"Fallo en la conexion.", JOptionPane.ERROR_MESSAGE);
			else {
				JOptionPane.showMessageDialog(this, "Tarjeta ingresada "
						+ usuario.getText() + ".", "Conexion exitosa.",
						JOptionPane.INFORMATION_MESSAGE);
				jpw.setText("");
				usuario.setText("");
				this.setVisible(false);
				ventana.habilitarAtm();

			}
		} else
			JOptionPane.showMessageDialog(this, "Error conectando ATM.",
					"Fallo en la conexion.", JOptionPane.ERROR_MESSAGE);

	}

	private void jButton1ActionPerformed(ActionEvent evt) {
		jpw.setText("");
		usuario.setText("");
		this.setVisible(false);
		ventana.cancelado();
	}

	public void setVentana(VentanaPrincipal v) {
		ventana = v;
	}

}
